package com.venus.lms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.venus.lms.mapper.BookMapper;
import com.venus.lms.mapper.BookWithUserMapper;
import com.venus.lms.mapper.LendRecordMapper;
import com.venus.lms.pojo.Book;
import com.venus.lms.pojo.BookWithUser;
import com.venus.lms.pojo.LendRecord;
import com.venus.lms.utils.Result;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 当前借阅记录控制层
 *
 * @author DQM
 */
@RestController
@RequestMapping("/bookwithuser")
public class BookWithUserController {

    @Resource
    BookWithUserMapper BookWithUserMapper;

    @Resource
    BookMapper bookMapper;

    @Resource
    LendRecordMapper lendRecordMapper;

    /**
     * 借书业务-添加一条当前借阅记录
     */
    @PostMapping("/insertNew")
    public Result<?> insertNew(@RequestBody BookWithUser BookWithUser) {
        BookWithUserMapper.insert(BookWithUser);
        return Result.success();
    }

    /**
     * 修改借书记录
     */
    @PostMapping
    public Result<?> update(@RequestBody BookWithUser BookWithUser) {
        UpdateWrapper<BookWithUser> updateWrapper = new UpdateWrapper<>();
        //根据图书编号和记录id进行查找
        updateWrapper.eq("isbn", BookWithUser.getIsbn()).eq("id", BookWithUser.getId());
        BookWithUserMapper.update(BookWithUser, updateWrapper);
        return Result.success();
    }

    /**
     * 归还一本书
     */
    @PostMapping("/deleteRecord")
    @Transactional
    public Result<?> deleteRecord(@RequestBody BookWithUser BookWithUser) {
        Date date = new java.util.Date();//获取当前时间对象，也可以直接传入Date的对象
        Map<String, Object> map = new HashMap<>();
        map.put("isbn", BookWithUser.getIsbn());
        map.put("id", BookWithUser.getId());
        //先在当前借阅记录表删除这一条记录
        BookWithUserMapper.deleteByMap(map);
        LambdaQueryWrapper<Book> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Book::getIsbn, BookWithUser.getIsbn());
        Book book = bookMapper.selectOne(wrapper);
        //再将图书表这一本书改为归还
        book.setStatus("1"); //归还
        bookMapper.updateById(book);
        LambdaQueryWrapper<LendRecord> wrapper1 = Wrappers.lambdaQuery();
        wrapper1.eq(LendRecord::getReaderId, BookWithUser.getId()).
                eq(LendRecord::getIsbn, BookWithUser.getIsbn()).
                eq(LendRecord::getStatus, "0");
        LendRecord lendRecord = lendRecordMapper.selectOne(wrapper1);
        //最后将历史借阅记录表记录改为归还，并添加归还时间
        lendRecord.setStatus("1");  //归还
        lendRecord.setReturnTime(date);
        lendRecordMapper.updateById(lendRecord);
        return Result.success();
    }

    /**
     * 图书批量归还
     */
    @PostMapping("/deleteRecords")
    @Transactional
    public Result<?> deleteRecords(@RequestBody List<BookWithUser> BookWithUsers) {
        Date date = new java.util.Date();//获取当前时间对象，也可以直接传入Date的对象
        for (BookWithUser curRecord : BookWithUsers) {
            Map<String, Object> map = new HashMap<>();
            map.put("isbn", curRecord.getIsbn());
            map.put("id", curRecord.getId());
            BookWithUserMapper.deleteByMap(map);
            LambdaQueryWrapper<Book> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(Book::getIsbn, curRecord.getIsbn());
            Book book = bookMapper.selectOne(wrapper);
            book.setStatus("1"); //归还
            bookMapper.updateById(book);
            LambdaQueryWrapper<LendRecord> wrapper1 = Wrappers.lambdaQuery();
            wrapper1.eq(LendRecord::getReaderId, curRecord.getId()).
                    eq(LendRecord::getIsbn, curRecord.getIsbn()).
                    eq(LendRecord::getStatus, "0");
            LendRecord lendRecord = lendRecordMapper.selectOne(wrapper1);
            //最后将历史借阅记录表记录改为归还，并添加归还时间
            lendRecord.setStatus("1");  //归还
            lendRecord.setReturnTime(date);
            lendRecordMapper.updateById(lendRecord);
        }
        return Result.success();
    }

    /**
     * 分页展示
     */
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search1,
                              @RequestParam(defaultValue = "") String search2,
                              @RequestParam(defaultValue = "") String search3) {
        LambdaQueryWrapper<BookWithUser> wrappers = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(search1)) {
            wrappers.like(BookWithUser::getIsbn, search1);
        }
        if (StringUtils.isNotBlank(search2)) {
            wrappers.like(BookWithUser::getBookName, search2);
        }
        if (StringUtils.isNotBlank(search3)) {
            wrappers.like(BookWithUser::getId, search3);
        }
        wrappers.orderByDesc(BookWithUser::getLendtime);   //按借阅时间降序排序
        Page<BookWithUser> BookPage = BookWithUserMapper.selectPage(new Page<>(pageNum, pageSize), wrappers);
        return Result.success(BookPage);
    }
}
